// #ifdef H5
var wx = require('jweixin-module')
// #endif
import {mapState} from 'vuex'
import {wxConfig} from "./wxConfig";
import queryParams from "../utils/queryParams";
export const pageShare = {
    mixins: [wxConfig],
    data() {
        return {
            shareParams: {
				title:'',
				desc:'',
				path:'',
				imgUrl:''
			},
            IOS_SHARE_AGAIN: null, 
			hasSetImgUrl:true, 
        }
    },
    computed: {
        ...mapState(['vuex_user', 'vuex_business', 'vuex_host']),
        
        getShareParams() {
            return {
                title: this.shareParams.title || (this.vuex_business.share_title || this.vuex_business.name),
                desc: this.shareParams.desc || 'Ta发现这个很不错，推荐看看！',
                imgUrl: this.shareParams.imgUrl || null,
            }
        }
    },
	mounted () {
		// #ifdef H5
		if(!this.customShareParams){
			this.onShareByH5()
		}
		// #endif
	},
	onShow () {
		this.iosShareShow()
	},
    methods: {
		shareOnshow () {
			this.iosShareShow()
		},
        onShareByH5(list) {
			return new Promise((resolve,reject)=>{
				// #ifdef H5
				this.$nextTick(() => {
					let arr = ['updateAppMessageShareData', 'updateTimelineShareData', 'onMenuShareWeibo']
					if(list){
						arr = arr.concat(list)
					}
					this.initWxConfig(arr).then(res => {
						let shareObj = {
							title:this.getShareParams.title,
							desc:this.getShareParams.desc,
							link:window.location.origin + '/' + this.queryShareParams(),
							imgUrl:this.getShareParams.imgUrl || ' ',
						}
						if (res.isiOS) this.IOS_SHARE_AGAIN = {...shareObj}
						wx.ready(function () {
							wx.updateAppMessageShareData({
								...shareObj
							})
							wx.onMenuShareWeibo({
								...shareObj
							})
							wx.updateTimelineShareData({
								...shareObj
							})
							resolve()
						});
						wx.error(function (err) {
							console.log(err);
						})
					}).catch(()=>{})
				})
				// #endif
				// #ifndef H5
				resolve()
				// #endif
			})
        },
        iosShareShow() {
            // #ifdef H5
            if (process.env.NODE_ENV === 'development') return
            if (this.IOS_SHARE_AGAIN) {
                let that = this
                wx.ready(function () { 
                    wx.updateAppMessageShareData({
                        ...that.IOS_SHARE_AGAIN,
                        success: function () {
					
                        }
                    })
                    wx.onMenuShareWeibo({
                        ...that.IOS_SHARE_AGAIN,
                        success: function () {
						
                        }
                    })
                    wx.updateTimelineShareData({
                        ...that.IOS_SHARE_AGAIN,
                        success: function () {
							
                        }
                    })
                });
            }
            // #endif
        },
        getShareAppMessage() {
            let params = this.getShareParams
            return {
                title: params.title,
                path: this.queryShareParams(),
                imageUrl: this.hasSetImgUrl?params.imgUrl:null
            }
        },
        queryShareParams() {
			let path = ''
			let params = {}
			if(this.shareParams.path){
				path = this.shareParams.path
				params = this.shareParams.query || {}
			}else{
				let routes = getCurrentPages();
				path = routes[routes.length - 1].route;
				params = routes[routes.length - 1].options
			}
            let paramsStr = queryParams({
                superior: this.getShareSuperior(),
                ...params
            }, true)
            return path + paramsStr
        },
        getShareSuperior() {
            return (this.vuex_user.type == '2' || this.vuex_user.type == '4') ? this.vuex_user.pk_id : '0'
        },
        getShareTimeline() {
            let params = this.getShareParams
            return {
				title: params.title,
				imageUrl: this.hasSetImgUrl?params.imgUrl:null,
				query: {
					ShareTimeline:1,
					sharePath:this.queryShareParams(),
				    superior: this.getShareSuperior(),
				}
			}
        }
    },
}
